package com.company.test30.CharSoftware;

import javax.swing.*;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;

/**
 * Created by v on 16-12-5.
 */
public class Client extends JApplet implements Runnable{
    private ChatPanel chatPanel=new ChatPanel();

    private Socket socket;
    private DataInputStream input;
    private DataOutputStream output;

    public Client(){
        add(chatPanel);
        Thread thread=new Thread(this);
        thread.start();
    }

    @Override
    public void init() {
        super.init();
        setSize(700,500);
    }

    @Override
    public void run() {
        try{
            socket=new Socket("localhost",8000);
            input=new DataInputStream(socket.getInputStream());
            output=new DataOutputStream(socket.getOutputStream());
            //启动一个监听器负责发送信息
            chatPanel.jtaInput.addKeyListener(
                    new MyKeyListener(chatPanel,output,"色情主播"));
            //启动一个线程负责接受信息
            Thread thread=new Thread(
                    new ReceptionMessage(chatPanel,input));
            thread.start();
        }catch (IOException ex){
            ex.printStackTrace();
        }
    }
}
